package jframe;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
@SuppressWarnings("serial")
public class Client extends JFrame {
 
    private JTextArea textAreaShow;
    private JTextField textFieldInput;
    private JButton buttonSend;
    private JScrollPane paneTop;
    private JPanel paneBottom;
    private Socket socket;
    private DataInputStream in;
    private DataOutputStream out;
    private String str;
    SendActionListener sendActionListener;
 
    public Client() {
        initFrame();
        connect();
    }
 
    private void initFrame() {
 
        setTitle("客户端");
        setSize(500, 400);
        setMinimumSize(new Dimension(250, 200));
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowExitListener());
 
        // 重写append方法，在前面加上日期和在末尾加上换行
        textAreaShow = new JTextArea() {
            @Override
            public void append(String str) {
                str = new Date().toString() + "   " + str + "\n";
                super.append(str);
            }
        };
        textAreaShow.setEditable(false);
        textAreaShow.setLineWrap(true);
        textAreaShow.setWrapStyleWord(true);
 
        SendActionListener sendActionListener = new SendActionListener();
        textFieldInput = new JTextField();
        textFieldInput.addActionListener(sendActionListener);
        buttonSend = new JButton("发送");
        buttonSend.addActionListener(new SendActionListener());
 
        paneTop = new JScrollPane(textAreaShow,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 
        paneBottom = new JPanel(new BorderLayout());
        paneBottom.add(textFieldInput, BorderLayout.CENTER);
        paneBottom.add(buttonSend, BorderLayout.EAST);
 
        add(paneTop, BorderLayout.CENTER);
        add(paneBottom, BorderLayout.SOUTH);
 
        setVisible(true);
    }
 
    // 连接到服务，并得到输入输出流
    private void connect() {
//        int port = 8015;
        int port = 8888;
        try {
//            socket = new Socket("192.168.1.6", port);
            socket = new Socket("localhost", port);
            textAreaShow.append("连接到服务,本地端口号" + socket.getLocalPort());
            setTitle("客户端" + socket.getLocalPort());
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
 
            String message;
            while ((message = in.readUTF()) != null) {
                textAreaShow.append(message);
            }
        } catch (IOException e) {
            textAreaShow.append("连接异常");
            close();
        }
    }
 
    // 释放资源
    private int close() {
        int exitCode = 0;
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
                exitCode = 1;
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                exitCode = 1;
            }
 
        }
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                exitCode = 1;
                e.printStackTrace();
            }
        }
        return exitCode;
    }
 
    // 响应关闭操作
    private class WindowExitListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent event) {
            System.exit(close());
        }
    }
 
    // 响应发送操作
    private class SendActionListener implements ActionListener {
 
        @Override
        public void actionPerformed(ActionEvent event) {
            str = textFieldInput.getText();
            try {
                out.writeUTF(str);
                textFieldInput.setText("");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
    }
 
    public static void main(String[] args) {
        new Client();
    }
 
}